package com.jingdun.sport.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingdun.sport.bean.model.*;
import com.jingdun.sport.dao.PrizeWidgetRelationMapper;
import com.jingdun.sport.dao.PrizesMapper;
import com.jingdun.sport.dao.UserPrizeWidgetRelationMapper;
import com.jingdun.sport.service.UserPrizeWidgetRelationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
@Slf4j
@AllArgsConstructor
public class UserPrizeWidgetRelationServiceImpl extends ServiceImpl<UserPrizeWidgetRelationMapper, UserPrizeWidgetRelation> implements UserPrizeWidgetRelationService {

    private UserPrizeWidgetRelationMapper userPrizeWidgetRelationMapper;

    @Autowired
    private PrizesMapper prizesMapper;

    @Autowired
    private PrizeWidgetRelationMapper prizeWidgetRelationMapper;

    public void addUserPrizeWidgetRelation(UserPrizeWidgetRelation userPrizeWidgetRelation) {
        userPrizeWidgetRelationMapper.addUserPrizeWidgetRelation(userPrizeWidgetRelation);
    }

    public UserPrizeWidgetRelation getUserPrizeWidgetRelationDetail(@Param("params") Map<String, Object> m) {
        return userPrizeWidgetRelationMapper.getUserPrizeWidgetRelationDetail(m);
    }

    public void updateUserPrizeWidgetRelation(UserPrizeWidgetRelation userPrizeWidgetRelation) {
        userPrizeWidgetRelationMapper.updateUserPrizeWidgetRelation(userPrizeWidgetRelation);
    }

    @Override
    public List getMyPackageDataList(Map<String, Object> m) {
        return null;
    }

    public Page<Map<String, Object>> getPaginationUserPrizeWidgetRelation(@Param("params") Map<String, Object> m) {
        //新建分页
        Page<Map<String, Object>> page = new Page<Map<String, Object>>((long) m.get("current"), (long) m.get("size"));
        //返回结果
        page.setRecords(this.baseMapper.getPaginationUserPrizeWidgetRelation(page, m));
        return page;
    }

    public List<PareGetMyPackageDataListResponse> getMyPackageDataList(String userId) {
        List<PareGetMyPackageDataListResponse> pareGetMyPackageDataListResponseList = new ArrayList<>();
//        1、获取指定用户的所有的奖品组件列表
        List<PrizeAndWidgetDto> prizeAndWidgetByUserIdList = prizesMapper.getPrizeAndWidgetByUserId(userId);
        if (!prizeAndWidgetByUserIdList.isEmpty()) {
//        2、根据奖品列表中的奖品id获取所有的奖品信息及对应奖品组件
            Set<Long> userPrizeIdSet = prizeAndWidgetByUserIdList.stream().map(PrizeAndWidgetDto::getPrizeId).collect(Collectors.toSet());
            List<Prizes> userAllPrizes = prizesMapper.selectBatchIds(userPrizeIdSet);
//        3、遍历奖品信息及对应奖品组件如果奖品信息中的奖品id等于用户所有奖品组建列表中的奖品id则插入奖品及其对应的组件
            userAllPrizes.stream().forEach(userPrize -> {
                PareGetMyPackageDataListResponse pareGetMyPackageDataListResponse = pareGetMyPackageDataListResponseList.stream().filter(o ->
                        o.getPrizeId().equals(userPrize.getId())
                ).findFirst().orElse(null);
                if (pareGetMyPackageDataListResponse == null) {
//                    第一次插入奖品
                    pareGetMyPackageDataListResponse = new PareGetMyPackageDataListResponse();
                    pareGetMyPackageDataListResponse.setPrizePic(userPrize.getPic());
                    pareGetMyPackageDataListResponse.setPrizeId(userPrize.getId());
                    List<GetMyPackageDataListResponse> getMyPackageDataListResponseList = new ArrayList<>();
                    pareGetMyPackageDataListResponse.setPrizeWidgetList(getMyPackageDataListResponseList);
                    pareGetMyPackageDataListResponse.setPrizeName(userPrize.getName());
//                    插入该奖品下的所有组件
                    Set<Long> userWidgetId = new HashSet<>();
                    for (PrizeAndWidgetDto prizeAndWidgetDto : prizeAndWidgetByUserIdList) {
                        if (prizeAndWidgetDto.getPrizeId().equals(userPrize.getId())) {
                            PrizeWidgetRelation prizeWidgetRelation = prizeWidgetRelationMapper.selectById(prizeAndWidgetDto.getPrizeWidgetId());
                            GetMyPackageDataListResponse getMyPackageDataListResponse = new GetMyPackageDataListResponse();
                            getMyPackageDataListResponse.setPic(prizeWidgetRelation.getPic());
                            getMyPackageDataListResponse.setPrizeWidgetId(prizeWidgetRelation.getId());
                            getMyPackageDataListResponse.setPrizeWidgetIdCount(prizeAndWidgetDto.getCount().intValue());
                            getMyPackageDataListResponse.setWidgetName(prizeWidgetRelation.getWidgetName());
                            getMyPackageDataListResponse.setWidgetNumber(prizeWidgetRelation.getWidgetNumber());
                            getMyPackageDataListResponseList.add(getMyPackageDataListResponse);
                            userWidgetId.add(prizeAndWidgetDto.getPrizeWidgetId());
                        }
                    }
//                  4、获取所有奖品对应的奖品组件逐个遍历判断用户对应的组件是否已经集齐
                    Set<Long> idSet = new HashSet<>();
                    idSet.add(userPrize.getId());
//                    这个奖品对应的所有奖品组件
                    List<PrizeWithWidgetDto> prizeWithWidgetDtoList = prizesMapper.getPrizeWithWidgetById(idSet);
//                    查找出第一个
                    PrizeWithWidgetDto prizeWithWidgetDto = prizeWithWidgetDtoList.stream().filter(prizeWWDto -> !userWidgetId.contains(prizeWWDto.getPrizeWidgetId())).findFirst().orElse(null);
                    if (prizeWithWidgetDto == null) {
                        pareGetMyPackageDataListResponse.setCanMerge(Boolean.TRUE);
                    } else {
                        pareGetMyPackageDataListResponse.setCanMerge(Boolean.FALSE);
                    }
                    pareGetMyPackageDataListResponseList.add(pareGetMyPackageDataListResponse);
                } else {
                    return;
                }
            });
        }
        return pareGetMyPackageDataListResponseList;
    }
}
